//
//  ZZTaskScrollView.swift
//  QMXK
//
//  Created by jenkin on 2018/12/10.
//  Copyright © 2018年 jenkin. All rights reserved.
//

import UIKit

class ZZTaskScrollView: UIScrollView {
    
    var screenH:CGFloat = CommenConst.KScreenH
    var dataList:ZZTaskListModel?
    var cellArray:Array<ZZTaskViewCell>!
    var drawType:Int! = -1
    var topIndexArray:Array!  = []
    var lowIndexArray:Array!  = []
    var contentView:ZZTaskContentView! = ZZTaskContentView.init()
    
    //var myLayer:CALayer = 
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.cellArray? = Array.init()
        self.setUI()
        
        let longPress:UILongPressGestureRecognizer = UILongPressGestureRecognizer.init(target: self, action: #selector(longAction(tap:)))
        self.addGestureRecognizer(longPress)
        
        let tap:UITapGestureRecognizer = UITapGestureRecognizer.init(target: self, action: #selector(tapAction(sender:)))
        self.addGestureRecognizer(tap)
    }
    
    @objc func longAction(tap: UITapGestureRecognizer){
        
        let point:CGPoint = tap.location(in: self)
        self.contentView.getLongpressY(x: point.x)
        if self.dataList?.data == nil || self.dataList?.data.count == 0{
            return
        }
        
        let w:CGFloat = CommenConst.lineCellW
        let index:Int = Int(point.x / w)
        
        if self.dataList?.data == nil{
            return;
        }
        
        let count:Int = (self.dataList?.data.count)!
        if index >= count || index < 0{
            return;
        }
        
        if tap.state == UIGestureRecognizer.State.began{
            self.taskInfoVIew?.isHidden = false
            self.taskInfoVIew?.taskModel = self.dataList?.data[index]
            
        }else{
            print("end")
            self.taskInfoVIew?.isHidden = true
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI(){
        
        if self.dataList == nil{
            return
        }
        
        if self.dataList?.data == nil{
            return
        }
        
        if self.cellArray == nil{
            self.cellArray = Array.init()
        }
        
        let count:Int = (self.dataList?.data.count)!
        let w:CGFloat = CommenConst.lineCellW
        let h:CGFloat = screenH/2
        var allMax:CGFloat = self.dataList?.allMax ?? 0
        var allMin:CGFloat = self.dataList?.allMin ?? 0
        for index in 0..<count{
            let taskModel:ZZTaskModel = self.dataList!.data[index]
            if allMax < taskModel.max{
                allMax = taskModel.max
            }
            
            if allMin > taskModel.min{
                allMin = taskModel.min
            }
        }
        
        
        self.dataList?.allMax = allMax
        self.dataList?.allMin = allMin
        for index in 0..<count{
            
            if (index + 1) > self.cellArray.count{
                let cell:ZZTaskViewCell = ZZTaskViewCell.init(frame: CGRect.init(x: 0, y: 0, width: w, height: h))
                self.cellArray.append(cell)
            }
            
            let cell:ZZTaskViewCell = self.cellArray[index]
            cell.tag = index
            cell.removeFromSuperview()
            self.addSubview(cell)
            let taskModel:ZZTaskModel = self.dataList!.data[index]
            taskModel.allMin = allMin
            taskModel.allMax = allMax
            cell.taskModel = taskModel
            cell.snp.makeConstraints { (make) in
                make.left.equalTo(CGFloat(index) * w)
                make.width.equalTo(w)
                make.height.equalTo(h)
                make.top.equalTo(0)
            }
        }
        
        let contentW:CGFloat =  w * CGFloat(count) + 200
        self.contentSize = CGSize.init(width: contentW, height: 0)
        
        self.contentView.removeFromSuperview()
        self.addSubview(self.contentView)
        self.contentView.dataList = self.dataList
        self.contentView.backgroundColor = UIColor.clear
        self.contentView.snp.makeConstraints { (make) in
            make.left.top.equalTo(0)
            make.width.equalTo(contentW)
            make.height.equalTo(h)
        }
    }
    
    lazy var taskInfoVIew:ZZTaskInfoVIew? = {
        let infoView:ZZTaskInfoVIew = ZZTaskInfoVIew.init(frame: CGRect.init(x: 100, y: 100, width: 200, height: 100))
        infoView.backgroundColor = UIColor.white
        infoView.isHidden = true
        self.superview!.addSubview(infoView)
        return infoView
    }()
    
    //0是高点  1是低点
    func drawLine(type:NSInteger){
        self.drawType = type
        self.topIndexArray = []
        self.lowIndexArray = []
    }
    
    @objc func tapAction(sender:UITapGestureRecognizer){
    
        if self.dataList?.data == nil || self.dataList?.data.count == 0{
            return
        }
        
        if self.drawType == -1{
            return
        }
        
        if self.drawType == 0 && self.topIndexArray.count >= 2{
            return
        }
        
        if self.drawType == 1 && self.lowIndexArray.count >= 2{
            return
        }
        
        let point:CGPoint = sender.location(in: self)
        let w:CGFloat = CommenConst.lineCellW
        let index:Int! = Int(point.x / w)
        //let index:Int! = Int((point.x +  w/2) / w) - 1
        let count:Int = (self.dataList?.data.count)!
        if index > (count - 1) || index < 0{
            return
        }
        
        if self.drawType == 0{
            self.topIndexArray.append(index)
            if self.topIndexArray.count == 2{
                //画线
                self.contentView.drawLineType(type: 0, indexArray: self.topIndexArray)
                self.drawType = -1
            }
        }
        
        if self.drawType == 1{
            self.lowIndexArray.append(index)
            if self.lowIndexArray.count == 2{
                //画线
                self.contentView.drawLineType(type: 1, indexArray: self.lowIndexArray)
                self.drawType = -1
            }
        }
    }
    
    
    //取消上一条线条
    func cancelLastLine(){
        self.contentView.cancelLastLine();
    }
}
